/*********************************************************************
FILE : frmYearlyReportView.cs
DESCRIPTION: Form frmYearlyReportView dung de hien bao cao.
VERSION:
PROGRAMMER : Pham Minh Tuan		 
 *********************************************************************/
using System; 
using System.Drawing;
using System.Collections; 
using System.ComponentModel; 
using System.Windows.Forms; 
using System.Data.SqlClient;
using System.Data; 
using CrystalDecisions.Shared; 
using CrystalDecisions.CrystalReports.Engine; 
using PointOfSale.Global;

namespace PointOfSale
{ 
	/// <summary> 
	/// Summary description for frmMonthlyReportView. 
	/// </summary> 
	public class frmYearlyReportView : System.Windows.Forms.Form 
	{ 	
		public int intYear;
		public string strDepartmentName;

		string report_file = ""; 
		ReportDocument crReportDocument = new ReportDocument();
		private CrystalDecisions.Windows.Forms.CrystalReportViewer crpViewer; 
		/// <summary> 
		/// Required designer variable. 
		/// </summary> 
		private System.ComponentModel.Container components = null; 

		public frmYearlyReportView() 
		{ 
			// 
			// Required for Windows Form Designer support 
			// 
			InitializeComponent(); 

			// 
			// TODO: Add any constructor code after InitializeComponent call 
			// 
		} 

		/// <summary> 
		/// Clean up any resources being used. 
		/// </summary> 
		protected override void Dispose( bool disposing ) 
		{ 
			if( disposing ) 
			{ 
				if (components != null) 
				{ 
					components.Dispose(); 
				} 
			} 
			base.Dispose( disposing ); 
		} 

#region Windows Form Designer generated code 
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor. 
		/// </summary> 
		private void InitializeComponent() 
		{ 
			this.crpViewer = new CrystalDecisions.Windows.Forms.CrystalReportViewer();
			this.SuspendLayout();
			// 
			// crpViewer
			// 
			this.crpViewer.ActiveViewIndex = -1;
			this.crpViewer.DisplayGroupTree = false;
			this.crpViewer.Dock = System.Windows.Forms.DockStyle.Fill;
			this.crpViewer.Location = new System.Drawing.Point(0, 0);
			this.crpViewer.Name = "crpViewer";
			this.crpViewer.ReportSource = null;
			this.crpViewer.ShowGroupTreeButton = false;
			this.crpViewer.Size = new System.Drawing.Size(712, 381);
			this.crpViewer.TabIndex = 0;
			// 
			// frmMonthlyReportView
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(712, 381);
			this.Controls.Add(this.crpViewer);
			this.Name = "frmMonthlyReportView";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Report View";
			this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
			this.Load += new System.EventHandler(this.frmYearlyReportView_Load);
			this.ResumeLayout(false);

		} 
#endregion 

		private DataColumn CreateNewColumn( string sColumnType, string sColumnCaption,string sColumnName)
		{
			DataColumn dtColumn = new DataColumn();			
			dtColumn.DataType = System.Type.GetType("System." + sColumnType);
			dtColumn.ColumnName = sColumnName;
			dtColumn.Caption = sColumnCaption;			
			dtColumn.AutoIncrement = false;			
			dtColumn.ReadOnly = false;
			dtColumn.Unique = false;
			return dtColumn;
		}

		private void frmYearlyReportView_Load(object sender, System.EventArgs e)
		{
			try
			{//load file report
				
			
			Report.Yearly.rptYearlySummaryReport crReportDocument = new Report.Yearly.rptYearlySummaryReport();
				// Su dung store procedure
				SqlCommand cmd = new SqlCommand("spYearlySummaryReport",clsGlobal.g_DataConn);
				cmd.CommandType = CommandType.StoredProcedure;

				//tao cac tham so cho store procedure
				SqlParameter myParmYear = cmd.Parameters.Add("@Year", SqlDbType.Int);
				SqlParameter myParmDepartmentName = cmd.Parameters.Add("@DepartmentName", SqlDbType.NVarChar);

				SqlParameter myParmTotal_Amount = cmd.Parameters.Add("@Total_Amount", SqlDbType.Float);
				SqlParameter myParmTotal_Tax = cmd.Parameters.Add("@Total_Tax", SqlDbType.Float);
				SqlParameter myParmTotal_DisAmount = cmd.Parameters.Add("@Total_DisAmount", SqlDbType.Float);
				SqlParameter myParmTotal_PaidAmount = cmd.Parameters.Add("@Total_PaidAmount", SqlDbType.Float);
				SqlParameter myParmTotal_PostAmount = cmd.Parameters.Add("@Total_PostAmount", SqlDbType.Float);
				SqlParameter myParmTotal_Balance = cmd.Parameters.Add("@Total_Balance", SqlDbType.Float);

				//Thiet lap gia tri va huong cho cac tham so cua store procedure
				//Gia tri cua cac tham so INPUT
				myParmYear.Value = intYear;
				myParmDepartmentName.Value  = this.strDepartmentName ;
				//Huong cua cac tham so
				myParmYear.Direction = ParameterDirection.Input;
				myParmDepartmentName.Direction = ParameterDirection.Input;

				myParmTotal_Amount.Direction = ParameterDirection.Output; 
				myParmTotal_Tax.Direction = ParameterDirection.Output;
				myParmTotal_DisAmount.Direction = ParameterDirection.Output;
				myParmTotal_PaidAmount.Direction = ParameterDirection.Output;
				myParmTotal_PostAmount.Direction = ParameterDirection.Output;
				myParmTotal_Balance.Direction = ParameterDirection.Output;

				//Thuc hien store procedure
				SqlDataReader myReader = cmd.ExecuteReader();
				
				//Tao DataTable de chua du lieu lay tu store procedure
				DataTable dtYearlyReport = new DataTable("spYearlySummaryReport");
				dtYearlyReport.Columns.Add(CreateNewColumn( "Int16","","MonthNo"));
				dtYearlyReport.Columns.Add(CreateNewColumn( "String","","strMonth"));
				dtYearlyReport.Columns.Add(CreateNewColumn( "Double","","Amount"));
				dtYearlyReport.Columns.Add(CreateNewColumn( "Double","","Tax"));
				dtYearlyReport.Columns.Add(CreateNewColumn( "Double","","DisAmount"));
				dtYearlyReport.Columns.Add(CreateNewColumn( "Double","","PaidAmount"));
				dtYearlyReport.Columns.Add(CreateNewColumn( "Double","","PostAmount"));
				dtYearlyReport.Columns.Add(CreateNewColumn( "Double","","Balance"));

				//Dua du lieu vao DataTable
				while (myReader.Read())
				{			
					DataRow newRow=dtYearlyReport.NewRow();
					newRow["MonthNo"]= myReader["MonthNo"];
					newRow["strMonth"]= myReader["strMonth"];
					newRow["Amount"]= myReader["Amount"];
					newRow["Tax"]= myReader["Tax"];
					newRow["DisAmount"]= myReader["DisAmount"];
					newRow["PaidAmount"]= myReader["PaidAmount"];
					newRow["PostAmount"]= myReader["PostAmount"];
					newRow["Balance"]= myReader["Balance"];
					dtYearlyReport.Rows.Add(newRow);
				}
				myReader.Close();	

				//Dua du lieu tu DataTable vao DataSet
				DataSet dsYearlyReport = new DataSet("spYearlySummaryReport");
				dsYearlyReport.Tables.Add(dtYearlyReport);
			
				//Lay du lieu tu DataSet dua vao Report
				crReportDocument.SetDataSource(dsYearlyReport);
			
				//Dua cac tham so vao Report
				crReportDocument.SetParameterValue("@CompanyName",clsGlobal.g_CompanyName);
				crReportDocument.SetParameterValue("@CompanyAddress",clsGlobal.g_CompanyAddress);
				crReportDocument.SetParameterValue("@CompanyInfo","Tel: " + clsGlobal.g_CompanyTel + "; "
					+ "Fax: " + clsGlobal.g_CompanyFax + "; "
					+ "Email:" + clsGlobal.g_CompanyEmail);
				crReportDocument.SetParameterValue("@CompanyWebsite","Website: " + clsGlobal.g_CompanyWebsite );
				if (this.strDepartmentName == "(All)")
				{
					crReportDocument.SetParameterValue("@Header","Yearly Summary Report (All Departments) For " 
						+ System.Convert.ToString(intYear));
				}
				else
				{
					crReportDocument.SetParameterValue("@Header","Yearly Summary Report (" 
						+ this.strDepartmentName + ") For " + System.Convert.ToString(intYear));
				}
				crReportDocument.SetParameterValue("@Total_Amount",myParmTotal_Amount.Value );
				crReportDocument.SetParameterValue("@Total_Tax",myParmTotal_Tax.Value );
				crReportDocument.SetParameterValue("@Total_DisAmount",myParmTotal_DisAmount.Value );
				crReportDocument.SetParameterValue("@Total_PaidAmount",myParmTotal_PaidAmount.Value );
				crReportDocument.SetParameterValue("@Total_PostAmount",myParmTotal_PostAmount.Value );
				crReportDocument.SetParameterValue("@Total_Balance",myParmTotal_Balance.Value );

				//Hien Report
				crpViewer.ReportSource = crReportDocument; 		
			}
			catch(Exception ex)
			{
				MessageBox.Show(ex.ToString());
			}
		}
	} 
} 